<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>eventtargettester.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_eventtargettester.js.html">eventtargettester.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview goog.events.EventTarget tester.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.events.eventTargetTester&#39;);
<a name="line20"></a>goog.setTestOnly(&#39;goog.events.eventTargetTester&#39;);
<a name="line21"></a>goog.provide(&#39;goog.events.eventTargetTester.KeyType&#39;);
<a name="line22"></a>goog.setTestOnly(&#39;goog.events.eventTargetTester.KeyType&#39;);
<a name="line23"></a>goog.provide(&#39;goog.events.eventTargetTester.UnlistenReturnType&#39;);
<a name="line24"></a>goog.setTestOnly(&#39;goog.events.eventTargetTester.UnlistenReturnType&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line28"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line29"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * Setup step for the test functions. This needs to be called from the
<a name="line34"></a> * test setUp.
<a name="line35"></a> * @param {Function} listenFn Function that, given the same signature
<a name="line36"></a> *     as goog.events.listen, will add listener to the given event
<a name="line37"></a> *     target.
<a name="line38"></a> * @param {Function} unlistenFn Function that, given the same
<a name="line39"></a> *     signature as goog.events.unlisten, will remove listener from
<a name="line40"></a> *     the given event target.
<a name="line41"></a> * @param {Function} unlistenByKeyFn Function that, given 2
<a name="line42"></a> *     parameters: src and key, will remove the corresponding
<a name="line43"></a> *     listener.
<a name="line44"></a> * @param {Function} listenOnceFn Function that, given the same
<a name="line45"></a> *     signature as goog.events.listenOnce, will add a one-time
<a name="line46"></a> *     listener to the given event target.
<a name="line47"></a> * @param {Function} dispatchEventFn Function that, given the same
<a name="line48"></a> *     signature as goog.events.dispatchEvent, will dispatch the event
<a name="line49"></a> *     on the given event target.
<a name="line50"></a> * @param {Function} removeAllFn Function that, given the same
<a name="line51"></a> *     signature as goog.events.removeAll, will remove all listeners
<a name="line52"></a> *     according to the contract of goog.events.removeAll.
<a name="line53"></a> * @param {Function} getListenersFn Function that, given the same
<a name="line54"></a> *     signature as goog.events.getListeners, will retrieve listeners.
<a name="line55"></a> * @param {Function} getListenerFn Function that, given the same
<a name="line56"></a> *     signature as goog.events.getListener, will retrieve the
<a name="line57"></a> *     listener object.
<a name="line58"></a> * @param {Function} hasListenerFn Function that, given the same
<a name="line59"></a> *     signature as goog.events.hasListener, will determine whether
<a name="line60"></a> *     listeners exist.
<a name="line61"></a> * @param {goog.events.eventTargetTester.KeyType} listenKeyType The
<a name="line62"></a> *     key type returned by listen call.
<a name="line63"></a> * @param {goog.events.eventTargetTester.UnlistenReturnType}
<a name="line64"></a> *     unlistenFnReturnType
<a name="line65"></a> *     Whether we should check return value from
<a name="line66"></a> *     unlisten call. If unlisten does not return a value, this should
<a name="line67"></a> *     be set to false.
<a name="line68"></a> */
<a name="line69"></a>goog.events.eventTargetTester.setUp = function(
<a name="line70"></a>    listenFn, unlistenFn, unlistenByKeyFn, listenOnceFn,
<a name="line71"></a>    dispatchEventFn, removeAllFn,
<a name="line72"></a>    getListenersFn, getListenerFn, hasListenerFn,
<a name="line73"></a>    listenKeyType, unlistenFnReturnType) {
<a name="line74"></a>  listen = listenFn;
<a name="line75"></a>  unlisten = unlistenFn;
<a name="line76"></a>  unlistenByKey = unlistenByKeyFn;
<a name="line77"></a>  listenOnce = listenOnceFn;
<a name="line78"></a>  dispatchEvent = dispatchEventFn;
<a name="line79"></a>  removeAll = removeAllFn;
<a name="line80"></a>  getListeners = getListenersFn;
<a name="line81"></a>  getListener = getListenerFn;
<a name="line82"></a>  hasListener = hasListenerFn;
<a name="line83"></a>  keyType = listenKeyType;
<a name="line84"></a>  unlistenReturnType = unlistenFnReturnType;
<a name="line85"></a>
<a name="line86"></a>  listeners = [];
<a name="line87"></a>  for (var i = 0; i &lt; goog.events.eventTargetTester.MAX_; i++) {
<a name="line88"></a>    listeners[i] = createListener();
<a name="line89"></a>  }
<a name="line90"></a>
<a name="line91"></a>  eventTargets = [];
<a name="line92"></a>  for (i = 0; i &lt; goog.events.eventTargetTester.MAX_; i++) {
<a name="line93"></a>    eventTargets[i] = new goog.events.EventTarget();
<a name="line94"></a>  }
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Teardown step for the test functions. This needs to be called from
<a name="line100"></a> * test teardown.
<a name="line101"></a> */
<a name="line102"></a>goog.events.eventTargetTester.tearDown = function() {
<a name="line103"></a>  for (var i = 0; i &lt; goog.events.eventTargetTester.MAX_; i++) {
<a name="line104"></a>    goog.dispose(eventTargets[i]);
<a name="line105"></a>  }
<a name="line106"></a>};
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * The type of key returned by key-returning functions (listen).
<a name="line111"></a> * @enum {number}
<a name="line112"></a> */
<a name="line113"></a>goog.events.eventTargetTester.KeyType = {
<a name="line114"></a>  /**
<a name="line115"></a>   * Returns number for key.
<a name="line116"></a>   */
<a name="line117"></a>  NUMBER: 0,
<a name="line118"></a>
<a name="line119"></a>  /**
<a name="line120"></a>   * Returns undefined (no return value).
<a name="line121"></a>   */
<a name="line122"></a>  UNDEFINED: 1
<a name="line123"></a>};
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * The type of unlisten function&#39;s return value.
<a name="line128"></a> */
<a name="line129"></a>goog.events.eventTargetTester.UnlistenReturnType = {
<a name="line130"></a>  /**
<a name="line131"></a>   * Returns boolean indicating whether unlisten is successful.
<a name="line132"></a>   */
<a name="line133"></a>  BOOLEAN: 0,
<a name="line134"></a>
<a name="line135"></a>  /**
<a name="line136"></a>   * Returns undefind (no return value).
<a name="line137"></a>   */
<a name="line138"></a>  UNDEFINED: 1
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Expando property used on &quot;listener&quot; function to determine if a
<a name="line144"></a> * listener has already been checked. This is what allows us to
<a name="line145"></a> * implement assertNoOtherListenerIsCalled.
<a name="line146"></a> * @type {string}
<a name="line147"></a> */
<a name="line148"></a>goog.events.eventTargetTester.ALREADY_CHECKED_PROP = &#39;__alreadyChecked&#39;;
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * Expando property used on &quot;listener&quot; function to record the number
<a name="line153"></a> * of times it has been called the last time assertListenerIsCalled is
<a name="line154"></a> * done. This allows us to verify that it has not been called more
<a name="line155"></a> * times in assertNoOtherListenerIsCalled.
<a name="line156"></a> */
<a name="line157"></a>goog.events.eventTargetTester.NUM_CALLED_PROP = &#39;__numCalled&#39;;
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * The maximum number of initialized event targets (in eventTargets
<a name="line162"></a> * array) and listeners (in listeners array).
<a name="line163"></a> * @type {number}
<a name="line164"></a> * @private
<a name="line165"></a> */
<a name="line166"></a>goog.events.eventTargetTester.MAX_ = 10;
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * Contains test event types.
<a name="line171"></a> * @enum {string}
<a name="line172"></a> */
<a name="line173"></a>var EventType = {
<a name="line174"></a>  A: goog.events.getUniqueId(&#39;a&#39;),
<a name="line175"></a>  B: goog.events.getUniqueId(&#39;b&#39;),
<a name="line176"></a>  C: goog.events.getUniqueId(&#39;c&#39;)
<a name="line177"></a>};
<a name="line178"></a>
<a name="line179"></a>
<a name="line180"></a>var listen, unlisten, unlistenByKey, listenOnce, dispatchEvent;
<a name="line181"></a>var removeAll, getListeners, getListener, hasListener;
<a name="line182"></a>var keyType, unlistenReturnType;
<a name="line183"></a>var eventTargets, listeners;
<a name="line184"></a>
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>/**
<a name="line188"></a> * Custom event object for testing.
<a name="line189"></a> * @constructor
<a name="line190"></a> * @extends {goog.events.Event}
<a name="line191"></a> */
<a name="line192"></a>var TestEvent = function() {
<a name="line193"></a>  goog.base(this, EventType.A);
<a name="line194"></a>};
<a name="line195"></a>goog.inherits(TestEvent, goog.events.Event);
<a name="line196"></a>
<a name="line197"></a>
<a name="line198"></a>/**
<a name="line199"></a> * Creates a listener that executes the given function (optional).
<a name="line200"></a> * @param {!Function=} opt_listenerFn The optional function to execute.
<a name="line201"></a> * @return {!Function} The listener function.
<a name="line202"></a> */
<a name="line203"></a>function createListener(opt_listenerFn) {
<a name="line204"></a>  return goog.testing.recordFunction(opt_listenerFn);
<a name="line205"></a>}
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * Asserts that the given listener is called numCount number of times.
<a name="line210"></a> * @param {!Function} listener The listener to check.
<a name="line211"></a> * @param {number} numCount The number of times. See also the times()
<a name="line212"></a> *     function below.
<a name="line213"></a> */
<a name="line214"></a>function assertListenerIsCalled(listener, numCount) {
<a name="line215"></a>  assertEquals(&#39;Listeners is not called the correct number of times.&#39;,
<a name="line216"></a>               numCount, listener.getCallCount());
<a name="line217"></a>  listener[goog.events.eventTargetTester.ALREADY_CHECKED_PROP] = true;
<a name="line218"></a>  listener[goog.events.eventTargetTester.NUM_CALLED_PROP] = numCount;
<a name="line219"></a>}
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * Asserts that no other listeners, other than those verified via
<a name="line224"></a> * assertListenerIsCalled, have been called since the last
<a name="line225"></a> * resetListeners().
<a name="line226"></a> */
<a name="line227"></a>function assertNoOtherListenerIsCalled() {
<a name="line228"></a>  goog.array.forEach(listeners, function(l, index) {
<a name="line229"></a>    if (!l[goog.events.eventTargetTester.ALREADY_CHECKED_PROP]) {
<a name="line230"></a>      assertEquals(
<a name="line231"></a>          &#39;Listeners &#39; + index + &#39; is unexpectedly called.&#39;,
<a name="line232"></a>          0, l.getCallCount());
<a name="line233"></a>    } else {
<a name="line234"></a>      assertEquals(
<a name="line235"></a>          &#39;Listeners &#39; + index + &#39; is unexpectedly called.&#39;,
<a name="line236"></a>          l[goog.events.eventTargetTester.NUM_CALLED_PROP], l.getCallCount());
<a name="line237"></a>    }
<a name="line238"></a>  });
<a name="line239"></a>}
<a name="line240"></a>
<a name="line241"></a>
<a name="line242"></a>/**
<a name="line243"></a> * Resets all listeners call count to 0.
<a name="line244"></a> */
<a name="line245"></a>function resetListeners() {
<a name="line246"></a>  goog.array.forEach(listeners, function(l) {
<a name="line247"></a>    l.reset();
<a name="line248"></a>    l[goog.events.eventTargetTester.ALREADY_CHECKED_PROP] = false;
<a name="line249"></a>  });
<a name="line250"></a>}
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/**
<a name="line254"></a> * The number of times a listener should have been executed. This
<a name="line255"></a> * exists to make assertListenerIsCalled more readable.  This is used
<a name="line256"></a> * like so: assertListenerIsCalled(listener, times(2));
<a name="line257"></a> * @param {number} n The number of times a listener should have been
<a name="line258"></a> *     executed.
<a name="line259"></a> * @return {number} The number n.
<a name="line260"></a> */
<a name="line261"></a>function times(n) {
<a name="line262"></a>  return n;
<a name="line263"></a>}
<a name="line264"></a>
<a name="line265"></a>
<a name="line266"></a>function testNoListener() {
<a name="line267"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line268"></a>  assertNoOtherListenerIsCalled();
<a name="line269"></a>}
<a name="line270"></a>
<a name="line271"></a>
<a name="line272"></a>function testOneListener() {
<a name="line273"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line274"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line275"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line276"></a>  assertNoOtherListenerIsCalled();
<a name="line277"></a>
<a name="line278"></a>  resetListeners();
<a name="line279"></a>
<a name="line280"></a>  dispatchEvent(eventTargets[0], EventType.B);
<a name="line281"></a>  dispatchEvent(eventTargets[0], EventType.C);
<a name="line282"></a>  assertNoOtherListenerIsCalled();
<a name="line283"></a>}
<a name="line284"></a>
<a name="line285"></a>
<a name="line286"></a>function testTwoListenersOfSameType() {
<a name="line287"></a>  var key1 = listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line288"></a>  var key2 = listen(eventTargets[0], EventType.A, listeners[1]);
<a name="line289"></a>
<a name="line290"></a>  if (keyType == goog.events.eventTargetTester.KeyType.NUMBER) {
<a name="line291"></a>    assertNotEquals(key1, key2);
<a name="line292"></a>  } else {
<a name="line293"></a>    assertUndefined(key1);
<a name="line294"></a>    assertUndefined(key2);
<a name="line295"></a>  }
<a name="line296"></a>
<a name="line297"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line298"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line299"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line300"></a>  assertNoOtherListenerIsCalled();
<a name="line301"></a>}
<a name="line302"></a>
<a name="line303"></a>
<a name="line304"></a>function testInstallingSameListeners() {
<a name="line305"></a>  var key1 = listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line306"></a>  var key2 = listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line307"></a>  var key3 = listen(eventTargets[0], EventType.B, listeners[0]);
<a name="line308"></a>
<a name="line309"></a>  if (keyType == goog.events.eventTargetTester.KeyType.NUMBER) {
<a name="line310"></a>    assertEquals(key1, key2);
<a name="line311"></a>    assertNotEquals(key1, key3);
<a name="line312"></a>  } else {
<a name="line313"></a>    assertUndefined(key1);
<a name="line314"></a>    assertUndefined(key2);
<a name="line315"></a>    assertUndefined(key3);
<a name="line316"></a>  }
<a name="line317"></a>
<a name="line318"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line319"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line320"></a>
<a name="line321"></a>  dispatchEvent(eventTargets[0], EventType.B);
<a name="line322"></a>  assertListenerIsCalled(listeners[0], times(2));
<a name="line323"></a>
<a name="line324"></a>  assertNoOtherListenerIsCalled();
<a name="line325"></a>}
<a name="line326"></a>
<a name="line327"></a>
<a name="line328"></a>function testScope() {
<a name="line329"></a>  listeners[0] = createListener(function(e) {
<a name="line330"></a>    assertEquals(&#39;Wrong scope with undefined scope&#39;, eventTargets[0], this);
<a name="line331"></a>  });
<a name="line332"></a>  listeners[1] = createListener(function(e) {
<a name="line333"></a>    assertEquals(&#39;Wrong scope with null scope&#39;, eventTargets[0], this);
<a name="line334"></a>  });
<a name="line335"></a>  var scope = {};
<a name="line336"></a>  listeners[2] = createListener(function(e) {
<a name="line337"></a>    assertEquals(&#39;Wrong scope with specific scope object&#39;, scope, this);
<a name="line338"></a>  });
<a name="line339"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line340"></a>  listen(eventTargets[0], EventType.A, listeners[1], false, null);
<a name="line341"></a>  listen(eventTargets[0], EventType.A, listeners[2], false, scope);
<a name="line342"></a>
<a name="line343"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line344"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line345"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line346"></a>  assertListenerIsCalled(listeners[2], times(1));
<a name="line347"></a>}
<a name="line348"></a>
<a name="line349"></a>
<a name="line350"></a>function testDispatchEventDoesNotThrowWithDisposedEventTarget() {
<a name="line351"></a>  goog.dispose(eventTargets[0]);
<a name="line352"></a>  assertTrue(dispatchEvent(eventTargets[0], EventType.A));
<a name="line353"></a>}
<a name="line354"></a>
<a name="line355"></a>
<a name="line356"></a>function testDispatchEventWithObjectLiteral() {
<a name="line357"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line358"></a>
<a name="line359"></a>  assertTrue(dispatchEvent(eventTargets[0], {type: EventType.A}));
<a name="line360"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line361"></a>  assertNoOtherListenerIsCalled();
<a name="line362"></a>}
<a name="line363"></a>
<a name="line364"></a>
<a name="line365"></a>function testDispatchEventWithCustomEventObject() {
<a name="line366"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line367"></a>
<a name="line368"></a>  var e = new TestEvent();
<a name="line369"></a>  assertTrue(dispatchEvent(eventTargets[0], e));
<a name="line370"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line371"></a>  assertNoOtherListenerIsCalled();
<a name="line372"></a>
<a name="line373"></a>  var actualEvent = listeners[0].getLastCall().getArgument(0);
<a name="line374"></a>
<a name="line375"></a>  assertEquals(e, actualEvent);
<a name="line376"></a>  assertEquals(eventTargets[0], actualEvent.target);
<a name="line377"></a>}
<a name="line378"></a>
<a name="line379"></a>
<a name="line380"></a>function testDisposingEventTargetRemovesListeners() {
<a name="line381"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line382"></a>  goog.dispose(eventTargets[0]);
<a name="line383"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line384"></a>
<a name="line385"></a>  assertNoOtherListenerIsCalled();
<a name="line386"></a>}
<a name="line387"></a>
<a name="line388"></a>
<a name="line389"></a>/**
<a name="line390"></a> * Unlisten/unlistenByKey should still work after disposal. There are
<a name="line391"></a> * many circumstances when this is actually necessary. For example, a
<a name="line392"></a> * user may have listened to an event target and stored the key
<a name="line393"></a> * (e.g. in a goog.events.EventHandler) and only unlisten after the
<a name="line394"></a> * target has been disposed.
<a name="line395"></a> */
<a name="line396"></a>function testUnlistenWorksAfterDisposal() {
<a name="line397"></a>  var key = listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line398"></a>  goog.dispose(eventTargets[0]);
<a name="line399"></a>  unlisten(eventTargets[0], EventType.A, listeners[1]);
<a name="line400"></a>  if (unlistenByKey) {
<a name="line401"></a>    unlistenByKey(eventTargets[0], key);
<a name="line402"></a>  }
<a name="line403"></a>}
<a name="line404"></a>
<a name="line405"></a>
<a name="line406"></a>function testRemovingListener() {
<a name="line407"></a>  var ret1 = unlisten(eventTargets[0], EventType.A, listeners[0]);
<a name="line408"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line409"></a>  var ret2 = unlisten(eventTargets[0], EventType.A, listeners[1]);
<a name="line410"></a>  var ret3 = unlisten(eventTargets[0], EventType.B, listeners[0]);
<a name="line411"></a>  var ret4 = unlisten(eventTargets[1], EventType.A, listeners[0]);
<a name="line412"></a>
<a name="line413"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line414"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line415"></a>
<a name="line416"></a>  var ret5 = unlisten(eventTargets[0], EventType.A, listeners[0]);
<a name="line417"></a>  var ret6 = unlisten(eventTargets[0], EventType.A, listeners[0]);
<a name="line418"></a>
<a name="line419"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line420"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line421"></a>
<a name="line422"></a>  assertNoOtherListenerIsCalled();
<a name="line423"></a>
<a name="line424"></a>  if (unlistenReturnType ==
<a name="line425"></a>      goog.events.eventTargetTester.UnlistenReturnType.BOOLEAN) {
<a name="line426"></a>    assertFalse(ret1);
<a name="line427"></a>    assertFalse(ret2);
<a name="line428"></a>    assertFalse(ret3);
<a name="line429"></a>    assertFalse(ret4);
<a name="line430"></a>    assertTrue(ret5);
<a name="line431"></a>    assertFalse(ret6);
<a name="line432"></a>  } else {
<a name="line433"></a>    assertUndefined(ret1);
<a name="line434"></a>    assertUndefined(ret2);
<a name="line435"></a>    assertUndefined(ret3);
<a name="line436"></a>    assertUndefined(ret4);
<a name="line437"></a>    assertUndefined(ret5);
<a name="line438"></a>    assertUndefined(ret6);
<a name="line439"></a>  }
<a name="line440"></a>}
<a name="line441"></a>
<a name="line442"></a>
<a name="line443"></a>function testCapture() {
<a name="line444"></a>  eventTargets[0].setParentEventTarget(eventTargets[1]);
<a name="line445"></a>  eventTargets[1].setParentEventTarget(eventTargets[2]);
<a name="line446"></a>
<a name="line447"></a>  eventTargets[9].setParentEventTarget(eventTargets[0]);
<a name="line448"></a>
<a name="line449"></a>  var ordering = 0;
<a name="line450"></a>  listeners[0] = createListener(
<a name="line451"></a>      function(e) {
<a name="line452"></a>        assertEquals(eventTargets[2], e.currentTarget);
<a name="line453"></a>        assertEquals(eventTargets[0], e.target);
<a name="line454"></a>        assertEquals(&#39;First capture listener is not called first&#39;, 0, ordering);
<a name="line455"></a>        ordering++;
<a name="line456"></a>      });
<a name="line457"></a>  listeners[1] = createListener(
<a name="line458"></a>      function(e) {
<a name="line459"></a>        assertEquals(eventTargets[1], e.currentTarget);
<a name="line460"></a>        assertEquals(eventTargets[0], e.target);
<a name="line461"></a>        assertEquals(&#39;2nd capture listener is not called 2nd&#39;, 1, ordering);
<a name="line462"></a>        ordering++;
<a name="line463"></a>      });
<a name="line464"></a>  listeners[2] = createListener(
<a name="line465"></a>      function(e) {
<a name="line466"></a>        assertEquals(eventTargets[0], e.currentTarget);
<a name="line467"></a>        assertEquals(eventTargets[0], e.target);
<a name="line468"></a>        assertEquals(&#39;3rd capture listener is not called 3rd&#39;, 2, ordering);
<a name="line469"></a>        ordering++;
<a name="line470"></a>      });
<a name="line471"></a>
<a name="line472"></a>  listen(eventTargets[2], EventType.A, listeners[0], true);
<a name="line473"></a>  listen(eventTargets[1], EventType.A, listeners[1], true);
<a name="line474"></a>  listen(eventTargets[0], EventType.A, listeners[2], true);
<a name="line475"></a>
<a name="line476"></a>  // These should not be called.
<a name="line477"></a>  listen(eventTargets[3], EventType.A, listeners[3], true);
<a name="line478"></a>
<a name="line479"></a>  listen(eventTargets[0], EventType.B, listeners[4], true);
<a name="line480"></a>  listen(eventTargets[0], EventType.C, listeners[5], true);
<a name="line481"></a>  listen(eventTargets[1], EventType.B, listeners[6], true);
<a name="line482"></a>  listen(eventTargets[1], EventType.C, listeners[7], true);
<a name="line483"></a>  listen(eventTargets[2], EventType.B, listeners[8], true);
<a name="line484"></a>  listen(eventTargets[2], EventType.C, listeners[9], true);
<a name="line485"></a>
<a name="line486"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line487"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line488"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line489"></a>  assertListenerIsCalled(listeners[2], times(1));
<a name="line490"></a>  assertNoOtherListenerIsCalled();
<a name="line491"></a>}
<a name="line492"></a>
<a name="line493"></a>
<a name="line494"></a>function testBubble() {
<a name="line495"></a>  eventTargets[0].setParentEventTarget(eventTargets[1]);
<a name="line496"></a>  eventTargets[1].setParentEventTarget(eventTargets[2]);
<a name="line497"></a>
<a name="line498"></a>  eventTargets[9].setParentEventTarget(eventTargets[0]);
<a name="line499"></a>
<a name="line500"></a>  var ordering = 0;
<a name="line501"></a>  listeners[0] = createListener(
<a name="line502"></a>      function(e) {
<a name="line503"></a>        assertEquals(eventTargets[0], e.currentTarget);
<a name="line504"></a>        assertEquals(eventTargets[0], e.target);
<a name="line505"></a>        assertEquals(&#39;First bubble listener is not called first&#39;, 0, ordering);
<a name="line506"></a>        ordering++;
<a name="line507"></a>      });
<a name="line508"></a>  listeners[1] = createListener(
<a name="line509"></a>      function(e) {
<a name="line510"></a>        assertEquals(eventTargets[1], e.currentTarget);
<a name="line511"></a>        assertEquals(eventTargets[0], e.target);
<a name="line512"></a>        assertEquals(&#39;2nd bubble listener is not called 2nd&#39;, 1, ordering);
<a name="line513"></a>        ordering++;
<a name="line514"></a>      });
<a name="line515"></a>  listeners[2] = createListener(
<a name="line516"></a>      function(e) {
<a name="line517"></a>        assertEquals(eventTargets[2], e.currentTarget);
<a name="line518"></a>        assertEquals(eventTargets[0], e.target);
<a name="line519"></a>        assertEquals(&#39;3rd bubble listener is not called 3rd&#39;, 2, ordering);
<a name="line520"></a>        ordering++;
<a name="line521"></a>      });
<a name="line522"></a>
<a name="line523"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line524"></a>  listen(eventTargets[1], EventType.A, listeners[1]);
<a name="line525"></a>  listen(eventTargets[2], EventType.A, listeners[2]);
<a name="line526"></a>
<a name="line527"></a>  // These should not be called.
<a name="line528"></a>  listen(eventTargets[3], EventType.A, listeners[3]);
<a name="line529"></a>
<a name="line530"></a>  listen(eventTargets[0], EventType.B, listeners[4]);
<a name="line531"></a>  listen(eventTargets[0], EventType.C, listeners[5]);
<a name="line532"></a>  listen(eventTargets[1], EventType.B, listeners[6]);
<a name="line533"></a>  listen(eventTargets[1], EventType.C, listeners[7]);
<a name="line534"></a>  listen(eventTargets[2], EventType.B, listeners[8]);
<a name="line535"></a>  listen(eventTargets[2], EventType.C, listeners[9]);
<a name="line536"></a>
<a name="line537"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line538"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line539"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line540"></a>  assertListenerIsCalled(listeners[2], times(1));
<a name="line541"></a>  assertNoOtherListenerIsCalled();
<a name="line542"></a>}
<a name="line543"></a>
<a name="line544"></a>
<a name="line545"></a>function testCaptureAndBubble() {
<a name="line546"></a>  eventTargets[0].setParentEventTarget(eventTargets[1]);
<a name="line547"></a>  eventTargets[1].setParentEventTarget(eventTargets[2]);
<a name="line548"></a>
<a name="line549"></a>  listen(eventTargets[0], EventType.A, listeners[0], true);
<a name="line550"></a>  listen(eventTargets[1], EventType.A, listeners[1], true);
<a name="line551"></a>  listen(eventTargets[2], EventType.A, listeners[2], true);
<a name="line552"></a>
<a name="line553"></a>  listen(eventTargets[0], EventType.A, listeners[3]);
<a name="line554"></a>  listen(eventTargets[1], EventType.A, listeners[4]);
<a name="line555"></a>  listen(eventTargets[2], EventType.A, listeners[5]);
<a name="line556"></a>
<a name="line557"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line558"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line559"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line560"></a>  assertListenerIsCalled(listeners[2], times(1));
<a name="line561"></a>  assertListenerIsCalled(listeners[3], times(1));
<a name="line562"></a>  assertListenerIsCalled(listeners[4], times(1));
<a name="line563"></a>  assertListenerIsCalled(listeners[5], times(1));
<a name="line564"></a>  assertNoOtherListenerIsCalled();
<a name="line565"></a>}
<a name="line566"></a>
<a name="line567"></a>
<a name="line568"></a>function testPreventDefaultByReturningFalse() {
<a name="line569"></a>  listeners[0] = createListener(function(e) { return false; });
<a name="line570"></a>  listeners[1] = createListener(function(e) { return true; });
<a name="line571"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line572"></a>  listen(eventTargets[0], EventType.A, listeners[1]);
<a name="line573"></a>
<a name="line574"></a>  var result = dispatchEvent(eventTargets[0], EventType.A);
<a name="line575"></a>  assertFalse(result);
<a name="line576"></a>}
<a name="line577"></a>
<a name="line578"></a>
<a name="line579"></a>function testPreventDefault() {
<a name="line580"></a>  listeners[0] = createListener(function(e) { e.preventDefault(); });
<a name="line581"></a>  listeners[1] = createListener(function(e) { return true; });
<a name="line582"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line583"></a>  listen(eventTargets[0], EventType.A, listeners[1]);
<a name="line584"></a>
<a name="line585"></a>  var result = dispatchEvent(eventTargets[0], EventType.A);
<a name="line586"></a>  assertFalse(result);
<a name="line587"></a>}
<a name="line588"></a>
<a name="line589"></a>
<a name="line590"></a>function testPreventDefaultAtCapture() {
<a name="line591"></a>  listeners[0] = createListener(function(e) { e.preventDefault(); });
<a name="line592"></a>  listeners[1] = createListener(function(e) { return true; });
<a name="line593"></a>  listen(eventTargets[0], EventType.A, listeners[0], true);
<a name="line594"></a>  listen(eventTargets[0], EventType.A, listeners[1], true);
<a name="line595"></a>
<a name="line596"></a>  var result = dispatchEvent(eventTargets[0], EventType.A);
<a name="line597"></a>  assertFalse(result);
<a name="line598"></a>}
<a name="line599"></a>
<a name="line600"></a>
<a name="line601"></a>function testStopPropagation() {
<a name="line602"></a>  eventTargets[0].setParentEventTarget(eventTargets[1]);
<a name="line603"></a>  eventTargets[1].setParentEventTarget(eventTargets[2]);
<a name="line604"></a>
<a name="line605"></a>  listeners[0] = createListener(function(e) { e.stopPropagation(); });
<a name="line606"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line607"></a>  listen(eventTargets[0], EventType.A, listeners[1]);
<a name="line608"></a>  listen(eventTargets[1], EventType.A, listeners[2]);
<a name="line609"></a>  listen(eventTargets[2], EventType.A, listeners[3]);
<a name="line610"></a>
<a name="line611"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line612"></a>
<a name="line613"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line614"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line615"></a>  assertNoOtherListenerIsCalled();
<a name="line616"></a>}
<a name="line617"></a>
<a name="line618"></a>
<a name="line619"></a>function testStopPropagation2() {
<a name="line620"></a>  eventTargets[0].setParentEventTarget(eventTargets[1]);
<a name="line621"></a>  eventTargets[1].setParentEventTarget(eventTargets[2]);
<a name="line622"></a>
<a name="line623"></a>  listeners[1] = createListener(function(e) { e.stopPropagation(); });
<a name="line624"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line625"></a>  listen(eventTargets[0], EventType.A, listeners[1]);
<a name="line626"></a>  listen(eventTargets[1], EventType.A, listeners[2]);
<a name="line627"></a>  listen(eventTargets[2], EventType.A, listeners[3]);
<a name="line628"></a>
<a name="line629"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line630"></a>
<a name="line631"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line632"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line633"></a>  assertNoOtherListenerIsCalled();
<a name="line634"></a>}
<a name="line635"></a>
<a name="line636"></a>
<a name="line637"></a>function testStopPropagation3() {
<a name="line638"></a>  eventTargets[0].setParentEventTarget(eventTargets[1]);
<a name="line639"></a>  eventTargets[1].setParentEventTarget(eventTargets[2]);
<a name="line640"></a>
<a name="line641"></a>  listeners[2] = createListener(function(e) { e.stopPropagation(); });
<a name="line642"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line643"></a>  listen(eventTargets[0], EventType.A, listeners[1]);
<a name="line644"></a>  listen(eventTargets[1], EventType.A, listeners[2]);
<a name="line645"></a>  listen(eventTargets[2], EventType.A, listeners[3]);
<a name="line646"></a>
<a name="line647"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line648"></a>
<a name="line649"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line650"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line651"></a>  assertListenerIsCalled(listeners[2], times(1));
<a name="line652"></a>  assertNoOtherListenerIsCalled();
<a name="line653"></a>}
<a name="line654"></a>
<a name="line655"></a>
<a name="line656"></a>function testStopPropagationAtCapture() {
<a name="line657"></a>  eventTargets[0].setParentEventTarget(eventTargets[1]);
<a name="line658"></a>  eventTargets[1].setParentEventTarget(eventTargets[2]);
<a name="line659"></a>
<a name="line660"></a>  listeners[0] = createListener(function(e) { e.stopPropagation(); });
<a name="line661"></a>  listen(eventTargets[2], EventType.A, listeners[0], true);
<a name="line662"></a>  listen(eventTargets[1], EventType.A, listeners[1], true);
<a name="line663"></a>  listen(eventTargets[0], EventType.A, listeners[2], true);
<a name="line664"></a>  listen(eventTargets[0], EventType.A, listeners[3]);
<a name="line665"></a>  listen(eventTargets[1], EventType.A, listeners[4]);
<a name="line666"></a>  listen(eventTargets[2], EventType.A, listeners[5]);
<a name="line667"></a>
<a name="line668"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line669"></a>
<a name="line670"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line671"></a>  assertNoOtherListenerIsCalled();
<a name="line672"></a>}
<a name="line673"></a>
<a name="line674"></a>
<a name="line675"></a>function testHandleEvent() {
<a name="line676"></a>  var obj = {};
<a name="line677"></a>  obj.handleEvent = goog.testing.recordFunction();
<a name="line678"></a>
<a name="line679"></a>  listen(eventTargets[0], EventType.A, obj);
<a name="line680"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line681"></a>
<a name="line682"></a>  assertEquals(1, obj.handleEvent.getCallCount());
<a name="line683"></a>}
<a name="line684"></a>
<a name="line685"></a>
<a name="line686"></a>function testListenOnce() {
<a name="line687"></a>  if (!listenOnce) {
<a name="line688"></a>    return;
<a name="line689"></a>  }
<a name="line690"></a>
<a name="line691"></a>  listenOnce(eventTargets[0], EventType.A, listeners[0], true);
<a name="line692"></a>  listenOnce(eventTargets[0], EventType.A, listeners[1]);
<a name="line693"></a>  listenOnce(eventTargets[0], EventType.B, listeners[2]);
<a name="line694"></a>
<a name="line695"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line696"></a>
<a name="line697"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line698"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line699"></a>  assertListenerIsCalled(listeners[2], times(0));
<a name="line700"></a>  assertNoOtherListenerIsCalled();
<a name="line701"></a>  resetListeners();
<a name="line702"></a>
<a name="line703"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line704"></a>
<a name="line705"></a>  assertListenerIsCalled(listeners[0], times(0));
<a name="line706"></a>  assertListenerIsCalled(listeners[1], times(0));
<a name="line707"></a>  assertListenerIsCalled(listeners[2], times(0));
<a name="line708"></a>
<a name="line709"></a>  dispatchEvent(eventTargets[0], EventType.B);
<a name="line710"></a>  assertListenerIsCalled(listeners[2], times(1));
<a name="line711"></a>  assertNoOtherListenerIsCalled();
<a name="line712"></a>}
<a name="line713"></a>
<a name="line714"></a>
<a name="line715"></a>function testUnlistenInListen() {
<a name="line716"></a>  listeners[1] = createListener(
<a name="line717"></a>      function(e) {
<a name="line718"></a>        unlisten(eventTargets[0], EventType.A, listeners[1]);
<a name="line719"></a>        unlisten(eventTargets[0], EventType.A, listeners[2]);
<a name="line720"></a>      });
<a name="line721"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line722"></a>  listen(eventTargets[0], EventType.A, listeners[1]);
<a name="line723"></a>  listen(eventTargets[0], EventType.A, listeners[2]);
<a name="line724"></a>  listen(eventTargets[0], EventType.A, listeners[3]);
<a name="line725"></a>
<a name="line726"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line727"></a>
<a name="line728"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line729"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line730"></a>  assertListenerIsCalled(listeners[2], times(0));
<a name="line731"></a>  assertListenerIsCalled(listeners[3], times(1));
<a name="line732"></a>  assertNoOtherListenerIsCalled();
<a name="line733"></a>  resetListeners();
<a name="line734"></a>
<a name="line735"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line736"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line737"></a>  assertListenerIsCalled(listeners[1], times(0));
<a name="line738"></a>  assertListenerIsCalled(listeners[2], times(0));
<a name="line739"></a>  assertListenerIsCalled(listeners[3], times(1));
<a name="line740"></a>  assertNoOtherListenerIsCalled();
<a name="line741"></a>}
<a name="line742"></a>
<a name="line743"></a>
<a name="line744"></a>function testUnlistenByKeyInListen() {
<a name="line745"></a>  if (!unlistenByKey) {
<a name="line746"></a>    return;
<a name="line747"></a>  }
<a name="line748"></a>
<a name="line749"></a>  var key1, key2;
<a name="line750"></a>  listeners[1] = createListener(
<a name="line751"></a>      function(e) {
<a name="line752"></a>        unlistenByKey(eventTargets[0], key1);
<a name="line753"></a>        unlistenByKey(eventTargets[0], key2);
<a name="line754"></a>      });
<a name="line755"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line756"></a>  key1 = listen(eventTargets[0], EventType.A, listeners[1]);
<a name="line757"></a>  key2 = listen(eventTargets[0], EventType.A, listeners[2]);
<a name="line758"></a>  listen(eventTargets[0], EventType.A, listeners[3]);
<a name="line759"></a>
<a name="line760"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line761"></a>
<a name="line762"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line763"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line764"></a>  assertListenerIsCalled(listeners[2], times(0));
<a name="line765"></a>  assertListenerIsCalled(listeners[3], times(1));
<a name="line766"></a>  assertNoOtherListenerIsCalled();
<a name="line767"></a>  resetListeners();
<a name="line768"></a>
<a name="line769"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line770"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line771"></a>  assertListenerIsCalled(listeners[1], times(0));
<a name="line772"></a>  assertListenerIsCalled(listeners[2], times(0));
<a name="line773"></a>  assertListenerIsCalled(listeners[3], times(1));
<a name="line774"></a>  assertNoOtherListenerIsCalled();
<a name="line775"></a>}
<a name="line776"></a>
<a name="line777"></a>
<a name="line778"></a>function testSetParentEventTarget() {
<a name="line779"></a>  assertNull(eventTargets[0].getParentEventTarget());
<a name="line780"></a>
<a name="line781"></a>  eventTargets[0].setParentEventTarget(eventTargets[1]);
<a name="line782"></a>  assertEquals(eventTargets[1], eventTargets[0].getParentEventTarget());
<a name="line783"></a>  assertNull(eventTargets[1].getParentEventTarget());
<a name="line784"></a>
<a name="line785"></a>  eventTargets[0].setParentEventTarget(null);
<a name="line786"></a>  assertNull(eventTargets[0].getParentEventTarget());
<a name="line787"></a>}
<a name="line788"></a>
<a name="line789"></a>
<a name="line790"></a>function testListenOnceAfterListenDoesNotChangeExistingListener() {
<a name="line791"></a>  if (!listenOnce) {
<a name="line792"></a>    return;
<a name="line793"></a>  }
<a name="line794"></a>
<a name="line795"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line796"></a>  listenOnce(eventTargets[0], EventType.A, listeners[0]);
<a name="line797"></a>
<a name="line798"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line799"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line800"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line801"></a>
<a name="line802"></a>  assertListenerIsCalled(listeners[0], times(3));
<a name="line803"></a>  assertNoOtherListenerIsCalled();
<a name="line804"></a>}
<a name="line805"></a>
<a name="line806"></a>
<a name="line807"></a>function testListenOnceAfterListenOnceDoesNotChangeExistingListener() {
<a name="line808"></a>  if (!listenOnce) {
<a name="line809"></a>    return;
<a name="line810"></a>  }
<a name="line811"></a>
<a name="line812"></a>  listenOnce(eventTargets[0], EventType.A, listeners[0]);
<a name="line813"></a>  listenOnce(eventTargets[0], EventType.A, listeners[0]);
<a name="line814"></a>
<a name="line815"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line816"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line817"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line818"></a>
<a name="line819"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line820"></a>  assertNoOtherListenerIsCalled();
<a name="line821"></a>}
<a name="line822"></a>
<a name="line823"></a>
<a name="line824"></a>function testListenAfterListenOnceRemoveOnceness() {
<a name="line825"></a>  if (!listenOnce) {
<a name="line826"></a>    return;
<a name="line827"></a>  }
<a name="line828"></a>
<a name="line829"></a>  listenOnce(eventTargets[0], EventType.A, listeners[0]);
<a name="line830"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line831"></a>
<a name="line832"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line833"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line834"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line835"></a>
<a name="line836"></a>  assertListenerIsCalled(listeners[0], times(3));
<a name="line837"></a>  assertNoOtherListenerIsCalled();
<a name="line838"></a>}
<a name="line839"></a>
<a name="line840"></a>
<a name="line841"></a>function testUnlistenAfterListenOnce() {
<a name="line842"></a>  if (!listenOnce) {
<a name="line843"></a>    return;
<a name="line844"></a>  }
<a name="line845"></a>
<a name="line846"></a>  listenOnce(eventTargets[0], EventType.A, listeners[0]);
<a name="line847"></a>  unlisten(eventTargets[0], EventType.A, listeners[0]);
<a name="line848"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line849"></a>
<a name="line850"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line851"></a>  listenOnce(eventTargets[0], EventType.A, listeners[0]);
<a name="line852"></a>  unlisten(eventTargets[0], EventType.A, listeners[0]);
<a name="line853"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line854"></a>
<a name="line855"></a>  listenOnce(eventTargets[0], EventType.A, listeners[0]);
<a name="line856"></a>  listen(eventTargets[0], EventType.A, listeners[0]);
<a name="line857"></a>  unlisten(eventTargets[0], EventType.A, listeners[0]);
<a name="line858"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line859"></a>
<a name="line860"></a>  listenOnce(eventTargets[0], EventType.A, listeners[0]);
<a name="line861"></a>  listenOnce(eventTargets[0], EventType.A, listeners[0]);
<a name="line862"></a>  unlisten(eventTargets[0], EventType.A, listeners[0]);
<a name="line863"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line864"></a>
<a name="line865"></a>  assertNoOtherListenerIsCalled();
<a name="line866"></a>}
<a name="line867"></a>
<a name="line868"></a>
<a name="line869"></a>function testRemoveAllWithType() {
<a name="line870"></a>  if (!removeAll) {
<a name="line871"></a>    return;
<a name="line872"></a>  }
<a name="line873"></a>
<a name="line874"></a>  listen(eventTargets[0], EventType.A, listeners[0], true);
<a name="line875"></a>  listen(eventTargets[0], EventType.A, listeners[1]);
<a name="line876"></a>  listen(eventTargets[0], EventType.C, listeners[2], true);
<a name="line877"></a>  listen(eventTargets[0], EventType.C, listeners[3]);
<a name="line878"></a>  listen(eventTargets[0], EventType.B, listeners[4], true);
<a name="line879"></a>  listen(eventTargets[0], EventType.B, listeners[5], true);
<a name="line880"></a>  listen(eventTargets[0], EventType.B, listeners[6]);
<a name="line881"></a>  listen(eventTargets[0], EventType.B, listeners[7]);
<a name="line882"></a>
<a name="line883"></a>  assertEquals(4, removeAll(eventTargets[0], EventType.B));
<a name="line884"></a>
<a name="line885"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line886"></a>  dispatchEvent(eventTargets[0], EventType.B);
<a name="line887"></a>  dispatchEvent(eventTargets[0], EventType.C);
<a name="line888"></a>
<a name="line889"></a>  assertListenerIsCalled(listeners[0], times(1));
<a name="line890"></a>  assertListenerIsCalled(listeners[1], times(1));
<a name="line891"></a>  assertListenerIsCalled(listeners[2], times(1));
<a name="line892"></a>  assertListenerIsCalled(listeners[3], times(1));
<a name="line893"></a>  assertNoOtherListenerIsCalled();
<a name="line894"></a>}
<a name="line895"></a>
<a name="line896"></a>
<a name="line897"></a>function testRemoveAll() {
<a name="line898"></a>  if (!removeAll) {
<a name="line899"></a>    return;
<a name="line900"></a>  }
<a name="line901"></a>
<a name="line902"></a>  listen(eventTargets[0], EventType.A, listeners[0], true);
<a name="line903"></a>  listen(eventTargets[0], EventType.A, listeners[1]);
<a name="line904"></a>  listen(eventTargets[0], EventType.C, listeners[2], true);
<a name="line905"></a>  listen(eventTargets[0], EventType.C, listeners[3]);
<a name="line906"></a>  listen(eventTargets[0], EventType.B, listeners[4], true);
<a name="line907"></a>  listen(eventTargets[0], EventType.B, listeners[5], true);
<a name="line908"></a>  listen(eventTargets[0], EventType.B, listeners[6]);
<a name="line909"></a>  listen(eventTargets[0], EventType.B, listeners[7]);
<a name="line910"></a>
<a name="line911"></a>  assertEquals(8, removeAll(eventTargets[0]));
<a name="line912"></a>
<a name="line913"></a>  dispatchEvent(eventTargets[0], EventType.A);
<a name="line914"></a>  dispatchEvent(eventTargets[0], EventType.B);
<a name="line915"></a>  dispatchEvent(eventTargets[0], EventType.C);
<a name="line916"></a>
<a name="line917"></a>  assertNoOtherListenerIsCalled();
<a name="line918"></a>}
<a name="line919"></a>
<a name="line920"></a>
<a name="line921"></a>function testGetListeners() {
<a name="line922"></a>  if (!getListeners) {
<a name="line923"></a>    return;
<a name="line924"></a>  }
<a name="line925"></a>
<a name="line926"></a>  listen(eventTargets[0], EventType.A, listeners[0], true);
<a name="line927"></a>  listen(eventTargets[0], EventType.A, listeners[1], true);
<a name="line928"></a>  listen(eventTargets[0], EventType.A, listeners[2]);
<a name="line929"></a>  listen(eventTargets[0], EventType.A, listeners[3]);
<a name="line930"></a>
<a name="line931"></a>  var l = getListeners(eventTargets[0], EventType.A, true);
<a name="line932"></a>  assertEquals(2, l.length);
<a name="line933"></a>  assertEquals(listeners[0], l[0].listener);
<a name="line934"></a>  assertEquals(listeners[1], l[1].listener);
<a name="line935"></a>
<a name="line936"></a>  l = getListeners(eventTargets[0], EventType.A, false);
<a name="line937"></a>  assertEquals(2, l.length);
<a name="line938"></a>  assertEquals(listeners[2], l[0].listener);
<a name="line939"></a>  assertEquals(listeners[3], l[1].listener);
<a name="line940"></a>
<a name="line941"></a>  l = getListeners(eventTargets[0], EventType.B, true);
<a name="line942"></a>  assertEquals(0, l.length);
<a name="line943"></a>}
<a name="line944"></a>
<a name="line945"></a>
<a name="line946"></a>function testGetListener() {
<a name="line947"></a>  if (!getListener) {
<a name="line948"></a>    return;
<a name="line949"></a>  }
<a name="line950"></a>
<a name="line951"></a>  listen(eventTargets[0], EventType.A, listeners[0], true);
<a name="line952"></a>
<a name="line953"></a>  assertNotNull(getListener(eventTargets[0], EventType.A, listeners[0], true));
<a name="line954"></a>  assertNull(
<a name="line955"></a>      getListener(eventTargets[0], EventType.A, listeners[0], true, {}));
<a name="line956"></a>  assertNull(getListener(eventTargets[1], EventType.A, listeners[0], true));
<a name="line957"></a>  assertNull(getListener(eventTargets[0], EventType.B, listeners[0], true));
<a name="line958"></a>  assertNull(getListener(eventTargets[0], EventType.A, listeners[1], true));
<a name="line959"></a>}
<a name="line960"></a>
<a name="line961"></a>
<a name="line962"></a>function testHasListener() {
<a name="line963"></a>  if (!hasListener) {
<a name="line964"></a>    return;
<a name="line965"></a>  }
<a name="line966"></a>
<a name="line967"></a>  assertFalse(hasListener(eventTargets[0]));
<a name="line968"></a>
<a name="line969"></a>  listen(eventTargets[0], EventType.A, listeners[0], true);
<a name="line970"></a>
<a name="line971"></a>  assertTrue(hasListener(eventTargets[0]));
<a name="line972"></a>  assertTrue(hasListener(eventTargets[0], EventType.A));
<a name="line973"></a>  assertTrue(hasListener(eventTargets[0], EventType.A, true));
<a name="line974"></a>  assertTrue(hasListener(eventTargets[0], undefined, true));
<a name="line975"></a>  assertFalse(hasListener(eventTargets[0], EventType.A, false));
<a name="line976"></a>  assertFalse(hasListener(eventTargets[0], undefined, false));
<a name="line977"></a>  assertFalse(hasListener(eventTargets[0], EventType.B));
<a name="line978"></a>  assertFalse(hasListener(eventTargets[0], EventType.B, true));
<a name="line979"></a>  assertFalse(hasListener(eventTargets[1]));
<a name="line980"></a>}
<a name="line981"></a>
<a name="line982"></a>
<a name="line983"></a>function testFiringEventBeforeDisposeInternalWorks() {
<a name="line984"></a>  /**
<a name="line985"></a>   * @extends {goog.events.EventTarget}
<a name="line986"></a>   * @constructor
<a name="line987"></a>   */
<a name="line988"></a>  var MockTarget = function() {
<a name="line989"></a>    goog.base(this);
<a name="line990"></a>  };
<a name="line991"></a>  goog.inherits(MockTarget, goog.events.EventTarget);
<a name="line992"></a>
<a name="line993"></a>  MockTarget.prototype.disposeInternal = function() {
<a name="line994"></a>    dispatchEvent(this, EventType.A);
<a name="line995"></a>    goog.base(this, &#39;disposeInternal&#39;);
<a name="line996"></a>  };
<a name="line997"></a>
<a name="line998"></a>  var t = new MockTarget();
<a name="line999"></a>  try {
<a name="line1000"></a>    listen(t, EventType.A, listeners[0]);
<a name="line1001"></a>    t.dispose();
<a name="line1002"></a>    assertListenerIsCalled(listeners[0], times(1));
<a name="line1003"></a>  } catch (e) {
<a name="line1004"></a>    goog.dispose(t);
<a name="line1005"></a>  }
<a name="line1006"></a>}
</pre>


</body>
</html>
